﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using log4net.Config;
using log4net.Appender;

namespace Ooganizer.Server
{
    [Serializable]
    public class LogServerClientWrapper
    {
        public LogServerClientWrapper(Type type)
        {
            _sinkName = @"tcp://localhost:8085/LogServer";
            Log = new ILogProxy(type);

            startLogger(type);
        }

        public LogServerClientWrapper(string sink, Type type)
        {
            _sinkName = sink;
            Log = new ILogProxy(type);

            startLogger(type);
        }

        private void startLogger(Type type)
        {
            BasicConfigurator.Configure(createRemoteAppender());
        }

        private RemotingAppender createRemoteAppender()
        {
            RemotingAppender remotingAppender = new RemotingAppender();

            remotingAppender.Sink = _sinkName;
            remotingAppender.Lossy = false;
            remotingAppender.BufferSize = 1;
            remotingAppender.OnlyFixPartialEventData = true;

            remotingAppender.ActivateOptions();

            return remotingAppender;
        }

        // Create a logger for use in this class
        public readonly ILogProxy Log;

        private string _sinkName;
    }
}
